using System;
using System.Collections.Generic;
using System.Text;
using GRE.Logics;
using System.IO;
using System.Xml.Serialization;

namespace GRE.Framework
{
    public class XMLHelper
    {
        public static string FileName = "C:\\VocabsBuilder\\allwords.xml";
        public static AllWords DeserializeFromFile()
        {
            try
            {
                if (File.Exists(FileName))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(AllWords));
                    FileStream fs = new FileStream(FileName, FileMode.Open);
                    AllWords wordDetails = (AllWords)serializer.Deserialize(fs);
                    return wordDetails;
                }
                else
                {
                    throw new Exception("File not found : '" + FileName + "'");
                }
            }
            catch (Exception excep)
            {
                throw excep;
            }
        }

        public static void SerializeToFile(AllWords wordDetails)
        {
            FileStream oFS = new FileStream(FileName, FileMode.Create, FileAccess.Write);
            StreamWriter oSW = new StreamWriter(oFS);
            try
            {
                Type oType = wordDetails.GetType();
                XmlSerializer oXMLSzer = new XmlSerializer(oType);
                oXMLSzer.Serialize(oSW, wordDetails);
                oSW.Flush();
            }
            catch (Exception excep)
            {
                throw excep;
            }
            finally
            {
                oSW.Close();
                oFS.Close();
                oFS = null;
                oSW = null;
            }
        }
    }
}
